package com.johnson.news.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import io.reactivex.functions.Consumer;

import android.os.Bundle;
import android.view.View;

import com.johnson.news.BR;
import com.johnson.news.R;
import com.johnson.news.databinding.ActivityOfficialWeiboBinding;
import com.johnson.news.model.bean.WeiboItem;
import com.johnson.news.model.remote.RemoteDataSource;
import com.youmbe.library.BaseBindingActivity;
import com.youmbe.library.BaseRecyclerAdapter;

import java.util.ArrayList;

public class OfficialWeiboActivity extends BaseBindingActivity<ActivityOfficialWeiboBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    BaseRecyclerAdapter<WeiboItem> adapter;
    @Override
    protected void init() {
        dataBinding.weiboList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseRecyclerAdapter<>(this, R.layout.adapter_official_weibo, com.johnson.news.BR.data);
        dataBinding.weiboList.setAdapter(adapter);
        adapter.setBindingViewClickListener(BR.onClickListener, onClickListener);


        RemoteDataSource.getRemoteDataSource()
                .getWeiboList(this)
                .subscribe(new Consumer<ArrayList<WeiboItem>>() {
                    @Override
                    public void accept(ArrayList<WeiboItem> weiboItems) throws Exception {
                        if(weiboItems.size() > 0) {
                            adapter.setData(weiboItems);
                        }
                    }
                });
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.weibo_root_layout:
                    Object object = view.getTag(R.id.adapter_tag_data);
                    if(object != null && object instanceof WeiboItem) {
                        Bundle bundle = new Bundle();
                        bundle.putString(WebViewActivity.intent_key_url, ((WeiboItem) object).getLink());
                        startActivity(WebViewActivity.class, bundle);
                    }
                    break;
            }
        }
    };

    @Override
    public int getContentViewId() {
        return R.layout.activity_official_weibo;
    }
}
